type ActionFlowParams = {
  onActionStart: () => void;
  onActionEnd: () => void;
};
export class ActionFlow {
  private onActionStart: () => void;
  private onActionEnd: () => void;
  private promiseResolve: (value?: any) => void = () => {};
  private promiseReject: (reason?: any) => void = () => {};
  constructor(initParams?: ActionFlowParams) {
    const { onActionStart, onActionEnd } = initParams || {
      onActionStart: () => {},
      onActionEnd: () => {},
    };
    this.onActionStart = onActionStart;
    this.onActionEnd = onActionEnd;
  }
  public actionEnd = (result?: any) => {
    this.onActionEnd();
    if (result === 'STOP') {
      this.promiseReject();
    } else {
      this.promiseResolve(result);
    }
  };
  public actionStart = () => {
    return new Promise((resolve, reject) => {
      this.onActionStart();
      this.promiseResolve = resolve;
      this.promiseReject = reject;
    });
  };
}
